import NProgress from 'nprogress';
import { Router } from 'vue-router';

import { usePermissionStoreHook } from '@/store/modules/permission';
const permissionStore = usePermissionStoreHook();
// 白名单路由
// const whiteList = ['/login'];
NProgress.configure({ showSpinner: false }); // 进度条
export function commonBeforeHook(router: Router) {
    router.beforeEach(async (to, from, next) => {
        NProgress.start();
        // 根据用户角色，动态生成路由
        const roles = ['ADMIN'];
        const accessRoutes = await permissionStore.generateRoutes(roles);
        // 调用 router.addRoutes 动态添加路由
        accessRoutes.forEach((route) => {
            router.addRoute(route);
        });
        // next();
        // const hasToken = localStorage.getItem('TOKEN');
        // if (hasToken) {
        //     if (to.path === '/login') {
        //         // 如果已登录，跳转首页
        //         next({ path: '/' });
        //         NProgress.done();
        //     } else {
        //         next();
        //     }
        // } else {
        //     /* 未登录可以访问白名单页面*/
        //     if (whiteList.indexOf(to.path) !== -1) {
        //         // in the free login whitelist, go directly
        //         next();
        //     } else {
        //         // other pages that do not have permission to access are redirected to the login page.
        //         next(`/login?redirect=${to.path}`);
        //         NProgress.done();
        //     }
        // }
        next();
        // console.info(`---------router to：${JSON.stringify(to)}`);
        // console.info(`---------router from：${JSON.stringify(from)}`);
    });
}
